package com.xl.baseMethod;

import lombok.extern.slf4j.Slf4j;

import static java.lang.Thread.sleep;

/**
 * JdkVersion: 1.8
 * Author: lei·xie
 * Created: 2024/8/5 0002 10:22
 * Description: 基本方法 join
 */
@Slf4j(topic = "b.Test7")
public class ThreadDemo07 {
    static int r1 = 0;
    static int r2 = 0;
    public static void main(String[] args) throws InterruptedException{
        test1();
    }

    private static void test1() throws InterruptedException{
        log.debug("开始");
        Thread t1 = new Thread(() -> {
            try {
                sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            r1 = 10;
        },"t1");
        Thread t2 = new Thread(() -> {
            try {
                sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            r2 = 10;
        },"t2");
        t1.start();
        t2.start();
        long start = System.currentTimeMillis();
        log.debug("join begin");
        t2.join();
        log.debug("t2 join end");
        t1.join();
        log.debug("t1 join end");
        long end = System.currentTimeMillis();
        log.debug("r1：{} r2:{} cost:{}",r1,r2,end-start);
        log.debug("结束");
    }
}
